/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.UserScreenConfigAddDTO;
import org.springblade.modules.admin.dto.UserScreenRescueDTO;
import org.springblade.modules.admin.dto.UserScreenTrappedDTO;
import org.springblade.modules.admin.enums.UserCommandConfigEnum;
import org.springframework.web.bind.annotation.*;
import org.springblade.modules.admin.entity.UserScreenConfig;
import org.springblade.modules.admin.service.IUserScreenConfigService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 人员-大屏配置 控制器
 *
 * @author ysq
 * @since 2023-12-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("sida-maintain-admin/userscreenconfig")
@Api(value = "人员-大屏配置", tags = "人员-大屏配置接口")
public class UserScreenConfigController extends BladeController {

	private final IUserScreenConfigService userScreenConfigService;

	/**
	 * 新增或修改 人员-大屏配置
	 */
	@PostMapping("/save/config")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "新增或修改线配置", notes = "传入userScreenConfig")
	public R<Boolean> saveConfig(@RequestBody UserScreenConfigAddDTO userScreenConfigAddDTO) {
		// 0. 数据准备与传参校验,获取当前用户是否已配置的信息
		Long currentUserId = AuthUtil.getUserId();
		Integer status = userScreenConfigAddDTO.getStatus();
		if(Func.isNull(status) || Func.isBlank(status.toString()) ){
			throw new ServiceException("Fail! Config status cannot be null or empty!");
		}
		// 配置表入参对象
		UserScreenConfig userScreenConfig = new UserScreenConfig();
		userScreenConfig.setUserId(currentUserId);
		userScreenConfig.setStatus(status);
		// 是否已存在的配置
		UserScreenConfig existOne = userScreenConfigService.getOne(new LambdaQueryWrapper<UserScreenConfig>()
			.eq(UserScreenConfig::getUserId, currentUserId)
			.eq(UserScreenConfig::getStatus, status)
			.eq(UserScreenConfig::getIsDeleted, 0)
		);
		// 是否存在：true：存在，false：不存在
		boolean flag = !Func.isNull(existOne);
		// 1. 根据状态，封装不同的配置DTO，并转换为JSON字符串
		if(status.equals(UserCommandConfigEnum.TRAPPED.getType())){
			// 1.1 困人救援分析
			UserScreenTrappedDTO userScreenTrappedDTO = new UserScreenTrappedDTO();
			userScreenTrappedDTO.setRate(userScreenConfigAddDTO.getRate());
			Double rate = userScreenConfigAddDTO.getRate();
			String rateString = rate + "%";
			userScreenTrappedDTO.setRateString(rateString);
			if(flag) {
				existOne.setConfig(JSON.toJSONString(userScreenTrappedDTO));
				return R.data(userScreenConfigService.updateById(existOne));
			}
			userScreenConfig.setConfig(JSON.toJSONString(userScreenTrappedDTO));
		}else if (status.equals(UserCommandConfigEnum.RESCUE.getType())) {
			// 1.2 救援用时趋势
			UserScreenRescueDTO userScreenRescueDTO = new UserScreenRescueDTO();
			userScreenRescueDTO.setResponseTime(userScreenConfigAddDTO.getResponseTime());
			userScreenRescueDTO.setArriveTime(userScreenConfigAddDTO.getArriveTime());
			userScreenRescueDTO.setRescueTime(userScreenConfigAddDTO.getRescueTime());
			if (flag) {
				existOne.setConfig(JSON.toJSONString(userScreenRescueDTO));
				return R.data(userScreenConfigService.updateById(existOne));
			}
			userScreenConfig.setConfig(JSON.toJSONString(userScreenRescueDTO));
		}
		// 2. 保存配置
		return R.data(userScreenConfigService.save(userScreenConfig));
	}

	/**
	 * 详情获取当前用户配置参考线
	 */
	@GetMapping("/get/config")
	@ApiOperationSupport(order = 30)
	@ApiOperation(value = "详情获取当前用户配置参考线", notes = "传入userScreenConfig")
	public R<JSONObject> getConfig(Integer status) {
		// 0. 数据准备
		Long currentUserId = AuthUtil.getUserId();
		if(Func.isNull(status) || Func.isBlank(status.toString()) ){
			throw new ServiceException("Fail! Config status cannot be null or empty!");
		}
		// 1. 获取当前用户配置
		UserScreenConfig existOne = userScreenConfigService.getOne(new LambdaQueryWrapper<UserScreenConfig>()
			.eq(UserScreenConfig::getUserId, currentUserId)
			.eq(UserScreenConfig::getStatus, status)
			.eq(UserScreenConfig::getIsDeleted, 0)
		);
		if(Func.isNull(existOne)){
			return R.data(new JSONObject());
		}
		// 2. 获取配置信息
		String config = existOne.getConfig();
		JSONObject jsonObject = JSON.parseObject(config);
		return R.data(jsonObject);
	}

}
